package com.moyu.mp.web;

import com.moyu.mp.util.SignUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/mp")
@Slf4j
public class AuthController {

    @RequestMapping(value = "/test")
    @ResponseBody
    public String test(){
        return "test-succ";
    }

    @RequestMapping(value = "/auth")
    @ResponseBody
    public String verifySignature(HttpServletRequest req, String signature, String timestamp, String nonce, String echostr) {
        if (!SignUtil.checkSignature(signature, timestamp, nonce)) {
            System.out.println("验证没通过");
            log.info("请求非法");
            return null;
        }
        switch (req.getMethod()) {
            case "GET":
                return echostr;
            default:
//                try {
//                    Map<String, String> map = MsgUtil.parseXml(req);
//                    String msgtype = map.get("MsgType");
//                    System.out.println("=============================" + map);
//                    if (MsgUtil.REQ_MESSAGE_TYPE_EVENT.equals(msgtype)) {
//                        return EventDispatcher.processEvent(map); //进入事件处理
//                    } else {
//                        return MsgDispatcher.processMessage(map); //进入消息处理
//                    }
//                } catch (Exception e) {
//                    log.error("AuthController error", e);
//                }
                log.info("method post---------------");
        }
        return null;
    }
}
